
/*DJI REMOTE RELATED*/

#include "stm32f4xx_hal.h"
#include "bsp_remote.h"
#include "math.h"
#include "stdlib.h"
//#include "Holder.h"



uint8_t rData[18];
uint8_t kR=1;
int16_t chasis_f0=0;
int16_t chasis_r0=0;
int16_t chasis_y0=0;

int16_t close_remote=0;
int16_t last_close_remote=0;
uint8_t remote_offline=0;

RC_Ctl_t RC_CtrlData;

struct _Holder Holder;

/******************************************************************************
* @fn RemoteDataProcess
*
* @brief resolution rc protocol data.
* @pData a point to rc receive buffer.
* @return None.
* @note RC_CtrlData is a global variable.you can deal with it in other place.
*/


void RemoteDateProcess(uint8_t *pData)
{
	//int16_t Delta_f0,Delta_r0;
	if(pData==NULL)
	{
		return;
	}
	
	RC_CtrlData.rc.ch0=((int16_t)pData[0]|((int16_t)pData[1]<<8))&0x07FF;
	RC_CtrlData.rc.ch1=(((uint16_t)pData[1]>>3)|((uint16_t)pData[2]<<5))&0x07FF;
	RC_CtrlData.rc.ch2=(((uint16_t)pData[2]>>6)|((uint16_t)pData[3]<<2)|((uint16_t)pData[4]<<10))&0x07FF;
	RC_CtrlData.rc.ch3=(((uint16_t)pData[4])>>1|((uint16_t)pData[5]<<7))&0x07FF;

	RC_CtrlData.rc.s1=((pData[5]>>4)&0x000C)>>2;
	RC_CtrlData.rc.s2=((pData[5]>>4)&0x0003);
	
	RC_CtrlData.mouse.x=((int16_t)pData[6])|((int16_t)pData[7]<<8);
	RC_CtrlData.mouse.y=((int16_t)pData[8])|((int16_t)pData[9]<<8);
	RC_CtrlData.mouse.z=((int16_t)pData[10])|((int16_t)pData[11]<<8);
	
	RC_CtrlData.mouse.press_l=pData[12];
	RC_CtrlData.mouse.press_r=pData[13];
	
	RC_CtrlData.key.v=((int16_t)pData[14]);//((int16_t)pData[15]<<8;
	
	
//	chasis_f0=kR*(RC_CtrlData.rc.ch1-1024);
//	chasis_r0=kR*(RC_CtrlData.rc.ch0-1024);
	//chasis_y0=*(RC_CtrlData.rc.ch2-1024);
//	abs(RC_CtrlData.rc.ch0)>10?(Delta_r0=(RC_CtrlData.rc.ch0-1024)-chasis_r0):(Delta_r0=0);
//	abs(RC_CtrlData.rc.ch1)>10?(Delta_f0=(RC_CtrlData.rc.ch1-1024)-chasis_f0):(Delta_f0=0);

//	if(chasis_f0>=0)
//	{
//		Delta_f0>0? (chasis_f0+=Delta_f0*f0_Sence):(chasis_f0+=Delta_f0*f0_Sence*2);
//	}
//	else
//	{
//		Delta_f0>0?(chasis_f0+=Delta_f0*f0_Sence*2):(chasis_f0+=Delta_f0*f0_Sence);
//	}
//	if(chasis_f0>Speed_Limit) chasis_f0=Speed_Limit;
//	if(chasis_f0<-Speed_Limit) chasis_f0=-Speed_Limit;
//	
//	
//	if(chasis_r0>=0)
//	{
//		Delta_r0>0? (chasis_r0+=Delta_r0*r0_Sence):(chasis_r0+=Delta_r0*r0_Sence*2);
//	}
//	else
//	{
//		Delta_r0>0? (chasis_r0+=Delta_r0*r0_Sence*2):(chasis_r0+=Delta_r0*r0_Sence);
//	}
//	if(chasis_r0>Speed_Limit) chasis_r0=Speed_Limit;
//	if(chasis_r0<-Speed_Limit) chasis_r0=-Speed_Limit;
	
	
	//Holder.Yaw_Target_Angle = Holder.Yaw_Target_Angle + (1024-RC_CtrlData.rc.ch2)*Yaw_Integ_Sense;//		   			
//	Holder.Pitch_Target_Angle+= (RC_CtrlData.rc.ch3-1024)*0.004f;		
//	if(Holder.Yaw_Target_Angle>180.0f) Holder.Yaw_Target_Angle-=360.0f;
//	if(Holder.Yaw_Target_Angle<=-180.f) Holder.Yaw_Target_Angle+=360.0f;
	
//	if(Holder.Pitch_Target_Angle > PITCH_POSITIVE_LIMIT_ANGLE)
//	{
//		Holder.Pitch_Target_Angle = PITCH_POSITIVE_LIMIT_ANGLE;
//	}
//	else if( Holder .Pitch_Target_Angle< PITCH_NEGTIVE_LIMIT_ANGLE)
//	{
//		Holder.Pitch_Target_Angle = PITCH_NEGTIVE_LIMIT_ANGLE;
//	}
//	else
//	 Holder.Pitch_Target_Angle= Holder.Pitch_Target_Angle ;		
	
}




//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//	RemoteDateProcess(rData);
//	//Chasis_Speed_Out();

//	HAL_UART_Receive_DMA(&huart1, rData, 18);
//}






